package com.project.service.impl;

import com.project.bean.CutPageBean;
import com.project.bean.InfoBean;
import com.project.bean.OrderBean;
import com.project.mapper.IOrderMapper;
import com.project.service.IOrderService;
import org.apache.ibatis.session.SqlSession;

import java.util.Arrays;
import java.util.List;

public class OrderServiceImpl extends BaseService implements IOrderService {
    @Override
    public CutPageBean<OrderBean> findByItem(int pageNO, String recrvier, String address) {
        CutPageBean<OrderBean> cutBean = new CutPageBean<>();
        SqlSession session = this.getSession();
        IOrderMapper mapper = session.getMapper(IOrderMapper.class);
        cutBean.setList(mapper.cutByItem((pageNO-1)*CutPageBean.PAGESIZE, CutPageBean.PAGESIZE,recrvier,address));
        cutBean.setCount(mapper.cutByCount(recrvier,address));
        session.close();
        return cutBean;
    }

    @Override
    public void add(OrderBean order, List<InfoBean> list) {
        SqlSession session = this.getSession();
        IOrderMapper mapper = session.getMapper(IOrderMapper.class);
        mapper.addOrder(order);
        mapper.addinfo(order.getId(),list);
        session.commit();
        session.close();
    }

    @Override
    public OrderBean findById(int id) {
        SqlSession session = this.getSession();
        IOrderMapper mapper = session.getMapper(IOrderMapper.class);
        OrderBean order = mapper.findById(id);
        session.close();
        return order;
    }

    public static void main(String[] args) {
        IOrderService service = new OrderServiceImpl();
//        System.out.println(service.findByItem(1,"", null));
//        service.add(new OrderBean("张迪","牛王庙101"), Arrays.asList(new InfoBean(101,2),new InfoBean(103,10)));
//        System.out.println(service.findById(3));
    }
}
